


#include <stdio.h>
#include <string>

class Point
{
public:
    int x1;
    int y1;
    int x2;
    int y2;
};


/*  0: oox      1: xoo      2: o        3: x        4: oo       5: oo       6: xo       7:  o       8: x
 *                             o           o           x            x           o          xo          oo
 *                             x           o
 *
 *  9:  x       10: ox      11: o       12: oxo     13: o       14: ox      15: xo      16: o       17:  o
 *     oo           o           ox                      x            o          o           xo          ox
 *                                                      o
 */

Point indexPoint(int x, int y, int index)
{
    const int offset[][4] = {
        //   x      y       x       y
        {   -1,     0,     -2,      0},     // 0
        {    1,     0,      2,      0},     // 1
        {    0,    -1,      0,     -2},     // 2
        {    0,     1,      0,      2},     // 3
        {    0,    -1,      1,     -1},     // 4
        {    0,    -1,     -1,     -1},     // 5
        {    1,     0,      1,      1},     // 6
        {    1,     0,      1,     -1},     // 7
        {    0,     1,      1,      1},     // 8
        {    0,     1,     -1,      1},     // 9
        {   -1,     0,     -1,      1},     // 10
        {   -1,     0,     -1,     -1},     // 11
        {   -1,     0,      1,      0},     // 12
        {    0,    -1,      0,      1},     // 13
        {   -1,     0,      0,      1},     // 14
        {    0,     1,      1,      0},     // 15
        {    1,     0,      0,     -1},     // 16
        {    0,    -1,     -1,      0},
    };

    Point   pt;
    pt.x1 = x + offset[index][0];
    pt.y1 = y + offset[index][1];
    pt.x2 = x + offset[index][2];
    pt.y2 = y + offset[index][3];
    // printf("(%d, %d) == %d ==> (%d, %d), (%d, %d)\n", y, x, index, pt.y1, pt.x1, pt.y2, pt.x2);
    return pt;
}

bool isPlacable(int map[5][5], int x, int y)
{
    if (x < 0 || x > 4 || y < 0 || y > 4)
        return false;
    if (map[y][x] != 0)
        return false;
    return true;
}

void showMap(int map[5][5])
{
    return;
    int i, j;
    for (i = 0; i < 5; i++) {
        for (j = 0; j < 5; j++) {
            printf("%d ", map[i][j]);
        }
        printf("\n");
    }
    printf("---------------------------\n");
}

static int steps = 0;
bool place(int map[5][5], int x, int y, int level, std::string& path)
{
    // printf("put: (%d, %d) for level %d\n", y, x, level);
    int     i;
    Point   pt;
    if (level == 1) {
        if (isPlacable(map, x, y)) {
            char    temp[1024];
            sprintf(temp, "(%d, %d),", y, x);
            path += temp;
            steps++;
            return true;
        }
        return false;
    }

    for (i = 0; i < 18; i++) {
        pt = indexPoint(x, y, i);
        if (!isPlacable(map, pt.x1, pt.y1) || !isPlacable(map, pt.x2, pt.y2)) {
            continue;
        }

        // printf("[%d]next point: (%d, %d), (%d, %d)\n", i, pt.y1, pt.x1, pt.y2, pt.x2);

        for (int t = 0; t < 2; t++) {
            if (t == 0) {
                // 1, 2, x
                if (!place(map, pt.x1, pt.y1, level - 1, path))
                    continue;
                map[pt.y1][pt.x1] = level - 1;
    showMap(map);
                if (!place(map, pt.x2, pt.y2, level - 1, path)) {
                    map[pt.y1][pt.x1] = 0;
                    continue;
                }
                map[pt.y2][pt.x2] = level - 1;
    showMap(map);
                if (!place(map, x, y, level - 1, path)) {
                    map[pt.y1][pt.x1] = 0;
                    map[pt.y2][pt.x2] = 0;
                    continue;
                }
                map[pt.y1][pt.x1] = 0;
                map[pt.y2][pt.x2] = 0;
                map[y][x] = level;
    showMap(map);
                return true;
            } else {
                // 2, 1, x
                if (!place(map, pt.x2, pt.y2, level - 1, path))
                    continue;
                map[pt.y2][pt.x2] = level - 1;
    showMap(map);
                if (!place(map, pt.x1, pt.y1, level - 1, path)) {
                    map[pt.y2][pt.x2] = 0;
                    continue;
                }
                map[pt.y1][pt.x1] = level - 1;
    showMap(map);
                if (!place(map, x, y, level - 1, path)) {
                    map[pt.y1][pt.x1] = 0;
                    map[pt.y2][pt.x2] = 0;
                    continue;
                }
                map[pt.y1][pt.x1] = 0;
                map[pt.y2][pt.x2] = 0;
                map[y][x] = level;
    showMap(map);
                return true;
            }
        }
    }
    return false;
}

int main()
{
    int map[5][5];
    int i;
    for (i = 0; i < 5; i++) {
        int ret = scanf("%d %d %d %d %d", &map[i][0], &map[i][1], &map[i][2], &map[i][3], &map[i][4]);
        if (ret != 5) {
            return -1;
        }
    }
    showMap(map);

    int y, x, level;
    int r = scanf("%d %d %d", &y, &x, &level);
    if (r != 3)
        return -1;

    std::string path;
    // bool ret = place(map, x, y, level, path);
    // printf("ret = %d, path = %s, steps = %d\n", ret, path.c_str(), steps);

    for (y = 0; y < 5; y++) {
        for (x = 0; x < 5; x++) {
            path = "";
            steps = 0;
            bool ret = place(map, x, y, 3, path);
            printf("(%d, %d) ret = %d, steps = %d\n", y, x, ret, steps);
        }
    }

    return 0;
}

